import { SpecType } from "./template.ts"

export const getStyle = (el: HTMLElement, prop: string) => {
  return parseInt(window.getComputedStyle(el, null).getPropertyValue(prop))
}

// 数组子集生成
export const SubSet = (origin: any[]) => {
  // console.log('origin', origin);
  
  // return origin.reduce(
  //   (subsets, value) => {
  //     return subsets.concat(subsets.map((set: any) => [value, ...set]))
  //   },
  //   [[]]
  // )

  const subSets = []
  
    // We will have 2^n possible combinations (where n is a length of original set).
    // It is because for every element of original set we will decide whether to include
    // it or not (2 options for each set element).
    const numberOfCombinations = 2 ** origin.length
  
    // Each number in binary representation in a range from 0 to 2^n does exactly what we need:
    // it shows by its bits (0 or 1) whether to include related element from the set or not.
    // For example, for the set {1, 2, 3} the binary number of 0b010 would mean that we need to
    // include only "2" to the current set.
    for (let combinationIndex = 0; combinationIndex < numberOfCombinations; combinationIndex += 1) {
      const subSet = []
  
      for (let setElementIndex = 0; setElementIndex < origin.length; setElementIndex += 1) {
        // Decide whether we need to include current element into the subset or not.
        if (combinationIndex & (1 << setElementIndex)) {
          subSet.push(origin[setElementIndex])
        }
      }
      // Add current subset to the list of all subsets.
      subSets.push(subSet)
    }
  
    return subSets
}

export function CreateSku(skuAttribute: SpecType[]) {
  console.log(skuAttribute);
  
  let attrValue: any[] = []
  skuAttribute.map((item) => attrValue.push(item.values))
  // 构建 sku
  let skus: any[] = []
  // 笛卡尔积算法
  skus = attrValue.reduce((prev, curr) => {
    let res: any[] = []
    prev.forEach((item: any) => {
      curr.forEach((currItem: any) => {
        // 将两个属性值合并为一个字符串，并存入结果集中
        let temp = item.name + ',' + currItem.name
        // 寻找销售属性指定的图片
        res.push({
          attrValue: temp,
          thumbnailUrl: item.thumbnailUrl || currItem.thumbnailUrl || '',
        })
      })
    })
    return res.map((item: any) => item.attrValue.split(','))
  })
  return skus
}

export const getNowTime = () => {
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return {
    year,
    month,
    day,
    hour,
    minute,
    second
  }
}

export const SessionStorage = {
  getItem: (name: any) => {
    const data = window.sessionStorage.getItem(name);
    try {
      if(data === null) {
        return
      }
      return JSON.parse(data);
    } catch (err) {
      return data;
    }
  },
  setItem: (name: any, value: any) => {
    if (typeof value === "object") {
      value = JSON.stringify(value);
    }

    window.sessionStorage.setItem(name, value);
  },
  removeItem: (name: any) => {
    window.sessionStorage.removeItem(name);
  }
};

export const LocalStorage = {
  getItem: (name: any): any => {
    const data = window.localStorage.getItem(name);
    try {
      if(data === null) {
        return
      }
      return JSON.parse(data);
    } catch (err) {
      return data;
    }
  },
  setItem: (name: any, value: any) => {
    if (typeof value === "object") {
      value = JSON.stringify(value);
    }

    window.localStorage.setItem(name, value);
  },
  removeItem: (name: any) => {
    window.localStorage.removeItem(name);
  }
};

